<?php
namespace NatalPHP\Util\Inflection;
/* SVN FILE: $Id: Inflector.php 53 2010-01-10 23:12:21Z Sadjow $ */
/**
 * Inflector class file.
 *
 * NatalPHP (tm) :  Sufficient and Simple Framework (http://www.trycatch.com.br)
 * Copyright 2010 Sadjow Medeiros Leão <sadjow[at]gmail.com>,
 * Waldson Patricio <waldsonpatricio[at]gmail.com
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright     Copyright 2010 Sadjow Medeiros Leão <sadjow[at]gmail.com>,
 *                Waldson Patricio <waldsonpatricio[at]gmail.com
 * @link          http://www.trycatch.com.br Natal(tm) Project
 * @package       NatalPHP
 * @subpackage    NatalPHP.NatalPHP.Util.Inflection
 * @since         NatalPHP(tm) v 0.1
 * @version       $Rev: 53 $
 * @modifiedby    $LastChangedBy: Sadjow $
 * @lastmodified  $Date: 2010-01-10 21:12:21 -0200 (dom, 10 jan 2010) $
 * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
 */

require_once(dirname(dirname(dirname(__FILE__))) . '/Util/NatalPhpSingleton.php' );

use NatalPHP\Util;
use NatalPHP\Util\Inflection;
/**
 * Class Inflector
 * @package NatalPHP
 * @package NatalPHP.NatalPHP.Util.Inflection
 */
class Inflector extends Util\NatalPHPSingleton {

    /**
     * Rules instance.
     * @var <type>
     */
    private $rules;

    /**
     * Sigularized words
     * @var array
     */
    private $singularized = array();

    /**
     * Pluralized words
     * @var pluralized
     */
    private $pluralized = array();

    /**
     * Sets the rules for inflections.
     * @param InflectorRules $rules Rules for Inflector
     */
    public function setRules($rules){
        $this->rules = $rules;
    }

    public function pluralize($word){
        if(isset($this->pluralized[$word])){
            return $this->pluralized[$word];
        }
        
        $regexUninflected = implode('|', $this->rules->getUninflected());
        $regexIrregular = implode('|', $this->rules->getIrregularPrural());


        if (preg_match('/^(' . $regexUninflected . ')$/i', $word, $regs)) {
            $_this->pluralized[$word] = $word;
            return $word;
        }

        if (preg_match('/(.*)\\b(' . $regexIrregular . ')$/i', $word, $regs)) {
            $_this->pluralized[$word] = $regs[1] . substr($word, 0, 1) . substr($irregular[strtolower($regs[2])], 1);
            return $_this->pluralized[$word];
        }

        foreach ($this->rules->getPrural() as $rule => $replacement) {
            if (preg_match($rule, $word)) {
                $_this->pluralized[$word] = preg_replace($rule, $replacement, $word);
                return $_this->pluralized[$word];
            }
        }
        
        $_this->pluralized[$word] = $word;
        return $word;
    }

    public function singularize($word){
        if(isset($this->singularized[$word])){
            return $this->singularized[$word];
        }

        $regexUninflected = implode('|',$this->rules->getUninflected());
        $regexIrregular =   implode('|', array_keys($this->rules->getIrregularSingular()));
        
        if (preg_match('/^(' . $regexUninflected . ')$/i', $word, $regs)) {
            $_this->singularized[$word] = $word;
            return $word;
        }

        if (preg_match('/(.*)\\b(' . $regexIrregular . ')$/i', $word, $regs)) {
            $_this->singularized[$word] = $regs[1] . substr($word, 0, 1) . substr($irregular[strtolower($regs[2])], 1);
            return $_this->singularized[$word];
        }

        foreach ($singularRules as $rule => $replacement) {
            if (preg_match($rule, $word)) {
                $_this->singularized[$word] = preg_replace($rule, $replacement, $word);
                return $_this->singularized[$word];
            }
        }
        
        $_this->singularized[$word] = $word;
        return $word;
    }

}